package com.ming.closeto.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.ming.closeto.R;
import com.ming.closeto.adapter.SpecialAdapter;
import com.ming.closeto.base.BaseActivity;
import com.ming.closeto.bean.SpecialBean;
import com.ming.closeto.entity.Constant;
import com.ming.closeto.utils.LogUtils;
import com.ming.closeto.utils.OkHttpUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class SpecialActivity extends BaseActivity {
    private RecyclerView mRecyclerView;
    private SpecialAdapter mSpecialAdapter;
    private List<SpecialBean> mList = new ArrayList<>();
    private String url;

    private ArrayList<String> mListBig = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_special);
        initView();
    }

    private void initView() {
        final Intent intent = getIntent();
        String title = intent.getStringExtra("title");
        if (!TextUtils.isEmpty(title)) {
            getSupportActionBar().setTitle(title);
        }
        url = intent.getStringExtra("url");
//        Toast.makeText(this, "title:" + title , Toast.LENGTH_SHORT).show();


        mRecyclerView = (RecyclerView) findViewById(R.id.mRecyclerView);
        mSpecialAdapter = new SpecialAdapter(this, mList);
        mRecyclerView.setLayoutManager(new GridLayoutManager(this,2));
        mRecyclerView.setAdapter(mSpecialAdapter);

        mSpecialAdapter.setOnItemClickListener(new SpecialAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position) {
                /**
                 * 1. 数据集
                 * 2. position
                 */
                Intent i = new Intent(SpecialActivity.this, GalleryActivity.class);
                i.putExtra("position", position);
                i.putStringArrayListExtra("bigUrl", mListBig);
                startActivity(i);
            }
        });

        initSpecial();
    }

    // 壁纸详情
    private void initSpecial() {
        if (!TextUtils.isEmpty(url)) {

            // 这里本应使用传过来url  但是因为API的变化，  这里只能从新填写一个能获取到 图片List 的url
            OkHttpUtils.get(Constant.WALLPAPER_URL_2, new OkHttpUtils.OnOkHttpCallback() {
                @Override
                public void onFailure(IOException e) {

                }

                @Override
                public void onFailure(String msg) {

                }

                @Override
                public void onSuccessful(String json) {
                    if (!TextUtils.isEmpty(json)) {
                        //LogUtils.i("json:" + json);
                        parsingJson(json);
                    }
                }
            });
        } else {
            Toast.makeText(this, "URL为空", Toast.LENGTH_SHORT).show();
        }
    }

    // 解析 JSON
    private void  parsingJson(String json) {
        try {
            JSONObject jsonObject = new JSONObject(json);
            JSONArray jsonArray = jsonObject.getJSONArray("results");
            for (int i=0; i<jsonArray.length(); i++) {
                JSONObject jsonO = (JSONObject) jsonArray.get(i);
                String small = jsonO.getString("url");
                String big = jsonO.getString("url");
                String down = jsonO.getString("desc");

                // 全部的大图片链接
                mListBig.add(big);

                SpecialBean bean = new SpecialBean();
                bean.setSmallUrl(small);
                bean.setBigUrl(big);
                bean.setDownloadSize(down);

                mList.add(bean);
            }
            mSpecialAdapter.notifyDataSetChanged();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
